<template>
  <el-card class="box-card">
    <el-form ref="form" :model="form" :rules="rules" label-width="150px" v-if="index===1">
      <el-form-item label="收款账户">
        <el-button type="primary" plain>一般对公帐户</el-button>
      </el-form-item>

      <el-form-item label="转入金额" prop="transAmt">
        <el-input-number v-model="form.transAmt" :precision="2" :step="100" :min="0"
                         :max="9999999"></el-input-number>
        元
      </el-form-item>

      <el-form-item label="付款账户">
        <el-button type="primary" plain>普通帐户 余额￥{{commonAmount}}</el-button>
      </el-form-item>

      <el-form-item label=" ">
        <el-button @click="submitForm" :disabled="disabled" type="primary" size="medium">提交</el-button>
      </el-form-item>
    </el-form>


    <el-form ref="form" :model="form" :rules="rules" label-width="150px" v-else-if="index===2">
      <el-form-item label="付款账户">
        <el-button type="primary" plain>一般对公帐户</el-button>
      </el-form-item>

      <el-form-item label="转出金额" prop="transAmt">
        <el-input-number v-model="form.transAmt" :precision="2" :step="100" :min="0"
                         :max="9999999"></el-input-number>
        元
      </el-form-item>

      <el-form-item label="收款账户">
        <el-button type="primary" plain>普通帐户 余额￥{{commonAmount}}</el-button>
      </el-form-item>

      <el-form-item label=" ">
        <el-button type="primary" @click="submitForm" :disabled="disabled" size="medium">提交</el-button>
      </el-form-item>
    </el-form>

    <el-form ref="form" :model="form" :rules="rules" label-width="150px" v-else-if="index===3">
      <el-form-item label="收款账户">
        <el-button type="primary" plain>普通帐户 余额￥{{commonAmount}}</el-button>
      </el-form-item>

      <el-form-item label="转入金额" prop="transAmt">
        <el-input-number v-model="form.transAmt" :precision="2" :step="100" :min="0"
                         :max="9999999"></el-input-number>
        元
      </el-form-item>

      <el-form-item label="付款账户">
        <el-radio v-model="defaultRadio" label="1" border>一般对公帐户</el-radio>
        <el-radio v-model="defaultRadio" label="2" border>营销账户 余额￥{{marketingAmount}}</el-radio>
      </el-form-item>

      <el-form-item label=" ">
        <el-button type="primary" :disabled="disabled"  @click="submitForm"  size="medium">提交</el-button>
      </el-form-item>
    </el-form>


    <el-form ref="form" :model="form" :rules="rules" label-width="150px" v-else-if="index===4">
      <el-form-item label="付款账户">
        <el-button type="primary" plain>普通帐户 余额￥{{commonAmount}}</el-button>
      </el-form-item>

      <el-form-item label="转出金额" prop="transAmt">
        <el-input-number v-model="form.transAmt" :precision="2" :step="100" :min="0"
                         :max="9999999"></el-input-number>
        元
      </el-form-item>

      <el-form-item label="收款账户">
        <el-radio v-model="defaultRadio" label="1" border>一般对公帐户</el-radio>
        <el-radio v-model="defaultRadio" label="2" border>营销账户 余额￥{{marketingAmount}}</el-radio>
      </el-form-item>

      <el-form-item label=" ">
        <el-button type="primary" :disabled="disabled"  @click="submitForm" size="medium">提交</el-button>
      </el-form-item>
    </el-form>

    <el-form ref="form" :model="form" :rules="rules" label-width="150px" v-else-if="index===5">
      <el-form-item label="付款账户">
        <el-button type="primary" plain>营销帐户 余额￥{{marketingAmount}}</el-button>
      </el-form-item>

      <el-form-item label="转出金额" prop="transAmt">
        <el-input-number v-model="form.transAmt" :precision="2" :step="100" :min="0"
                         :max="9999999"></el-input-number>
        元
      </el-form-item>

      <el-form-item label="收款账户">
        <el-button type="primary" plain>普通帐户 余额￥{{commonAmount}}</el-button>
      </el-form-item>

      <el-form-item label=" ">
        <el-button type="primary" :disabled="disabled"  @click="submitForm" size="medium">提交</el-button>
      </el-form-item>
    </el-form>

    <el-form ref="form" :model="form" :rules="rules" label-width="150px" v-else-if="index===6">
      <el-form-item label="收款账户">
        <el-button type="primary" plain>营销帐户 余额￥{{marketingAmount}}</el-button>
      </el-form-item>

      <el-form-item label="转入金额" prop="transAmt">
        <el-input-number v-model="form.transAmt" :precision="2" :step="100" :min="0"
                         :max="9999999"></el-input-number>
        元
      </el-form-item>

      <el-form-item label="付款账户">
        <el-button type="primary" plain>普通帐户 余额￥{{commonAmount}}</el-button>
      </el-form-item>

      <el-form-item label=" ">
        <el-button type="primary" :disabled="disabled"  @click="submitForm" size="medium">提交</el-button>
      </el-form-item>
    </el-form>

  </el-card>

</template>

<script>
  import {platformRecharge, platformTake, commonRecharge, commonTake} from "@/api/main/financial/fund";
  export default {
    props: {
      index: {
        type: Number,
        require: true
      },
      commonFund: {
        type: String
      },
      marketingFund: {
        type: String
      }
    },
    data() {
      let checkTransAmt = (rule, value, callback) => {
        console.log(value);
        if (value <= 0) {
          callback(new Error('金额必须大于0'));
        }else {
          callback();
        }
      };
      return {
        disabled:false,
        defaultRadio: '1',
        form: {
          transAmt: 0
        },
        rules: {
          transAmt: [
            {required: true},
            { validator: checkTransAmt, trigger: 'blur' }
          ],
        }
      }
    },
    computed: {
      commonAmount: function () {
        let result=0;
        switch (this.index) {
          case 1:
            result=(Number(this.commonFund)-this.form.transAmt).toFixed(2);
            break;
          case 2:
            result=(Number(this.commonFund)+this.form.transAmt).toFixed(2);
          case 3:
            result=(Number(this.commonFund)+this.form.transAmt).toFixed(2);
            break;
          case 4:
            result=(Number(this.commonFund)-this.form.transAmt).toFixed(2);
            break;
          case 5:
            result=(Number(this.commonFund)+this.form.transAmt).toFixed(2);
            break;
          case 6:
            result=(Number(this.commonFund)-this.form.transAmt).toFixed(2);
            break;

        }
        return result;
      },
      marketingAmount: function () {
        let result=0;
        switch (this.index) {
          case 3:
            if(this.defaultRadio==='2'){
              result=(Number(this.marketingFund)-this.form.transAmt).toFixed(2);
            }else{
              result=Number(this.marketingFund);
            }
            break;
          case 4:
            if(this.defaultRadio==='2'){
              result=(Number(this.marketingFund)+this.form.transAmt).toFixed(2);
            }else{
              result=Number(this.marketingFund);
            }
            break;
          case 5:
            result=(Number(this.marketingFund)-this.form.transAmt).toFixed(2);
            break;
          case 6:
            result=(Number(this.marketingFund)+this.form.transAmt).toFixed(2);
            break;

        }
        return result;
      }
    },
    created() {
      console.log(this.commonFund);
      console.log(this.marketingFund);
    },
    methods:{
      submitForm() {
        let _this=this;
        this.disabled = true;
        this.$refs["form"].validate(valid => {
          if (valid) {
            switch (this.index) {
              case 1:
                this.$confirm('是否确定转入资金', "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "info"
                }).then(function() {
                  platformTake(_this.form.transAmt).then(response => {
                    if (response.code === 200) {
                      _this.msgSuccess("转入成功");
                      setTimeout(()=>{
                        _this.$emit('goBack','platformRefresh');
                      },1000);
                    } else {
                      _this.msgError(response.msg);
                    }
                  });
                }).catch(function() {
                  _this.disabled = false;
                });

                break;
              case 2:
                this.$confirm('是否确定转出资金', "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "info"
              }).then(function() {
                platformRecharge(_this.form.transAmt).then(response => {
                  if (response.code === 200) {
                    _this.msgSuccess("转出成功");
                    setTimeout(()=>{
                      _this.$emit('goBack','platformRefresh');
                    },1000);
                  } else {
                    _this.msgError(response.msg);
                  }
                });
              }).catch(function() {
                _this.disabled = false;
              });
                break;
              case 3:
                if(this.defaultRadio==='1'){
                  this.$confirm('是否确定转入资金', "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "info"
                  }).then(function() {
                    platformRecharge(_this.form.transAmt).then(response => {
                      if (response.code === 200) {
                        _this.msgSuccess("转入成功");
                        setTimeout(()=>{
                          _this.$emit('goBack','platformRefresh');
                        },1000);
                      } else {
                        _this.msgError(response.msg);
                      }
                    });
                  }).catch(function() {
                    _this.disabled = false;
                  });
                }else{
                  this.$confirm('是否确定转入资金', "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "info"
                  }).then(function() {
                    commonRecharge(_this.form.transAmt).then(response => {
                      if (response.code === 200) {
                        _this.msgSuccess("转入成功");
                        setTimeout(()=>{
                          _this.$emit('goBack','platformRefresh');
                        },1000);
                      } else {
                        _this.msgError(response.msg);
                      }
                    });
                  }).catch(function() {
                    _this.disabled = false;
                  });
                }
                break;
              case 4:
                if(this.defaultRadio==='1'){
                  this.$confirm('是否确定转出资金', "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "info"
                  }).then(function() {
                    platformTake(_this.form.transAmt).then(response => {
                      if (response.code === 200) {
                        _this.msgSuccess("转出成功");
                        setTimeout(()=>{
                          _this.$emit('goBack','platformRefresh');
                        },1000);
                      } else {
                        _this.msgError(response.msg);
                      }
                    });
                  }).catch(function() {
                    _this.disabled = false;
                  });
                }else{
                  this.$confirm('是否确定转出资金', "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "info"
                  }).then(function() {
                    commonTake(_this.form.transAmt).then(response => {
                      if (response.code === 200) {
                        _this.msgSuccess("转出成功");
                        setTimeout(()=>{
                          _this.$emit('goBack','platformRefresh');
                        },1000);
                      } else {
                        _this.msgError(response.msg);
                      }
                    });
                  }).catch(function() {
                    _this.disabled = false;
                  });
                }
                break;
              case 5:
                this.$confirm('是否确定转出资金', "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "info"
                }).then(function() {
                  commonRecharge(_this.form.transAmt).then(response => {
                    if (response.code === 200) {
                      _this.msgSuccess("转出成功");
                      setTimeout(()=>{
                        _this.$emit('goBack','platformRefresh');
                      },1000);
                    } else {
                      _this.msgError(response.msg);
                    }
                  });
                }).catch(function() {
                  _this.disabled = false;
                });
                break;
              case 6:
                this.$confirm('是否确定转入资金', "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "info"
                }).then(function() {
                commonTake(_this.form.transAmt).then(response => {
                  if (response.code === 200) {
                    _this.msgSuccess("转入成功");
                    setTimeout(()=>{
                      _this.$emit('goBack','platformRefresh');
                    },1000);
                  } else {
                    _this.msgError(response.msg);
                  }
                });
                }).catch(function() {
                  _this.disabled = false;
                });
                break;
            }





          }
        });
      },
    }
  }
</script>

<style scoped>

</style>
